/*
 * @lc app=leetcode id=20 lang=cpp
 *
 * [20] Valid Parentheses
 */
class Solution {
public:
    bool isValid(string s) {
        int len = s.size();
        if (len < 0 || len&0x01) {
            return false;
        }

        deque<char> stack;
        for (auto c : s) {
            if (stack.empty()) {
                if (c == ']' || c == ')' || c == '}') {
                    return false;
                } else {
                    stack.push_back(c);
                }
            } else {
                auto t = stack.back();

                if ((t=='[' && c == ']') || 
                        (t=='{' && c == '}') || 
                        (t=='(' && c == ')')) {
                    stack.pop_back();
                } else {
                    stack.push_back(c);
                }
            }
        }
        return stack.empty();
    }
};

